package dh.mybatis;

import dh.mybatis.po.User;

import java.sql.*;

/**
 * @ClassName: Main
 * @Description: TODO
 * @Author: DaiHao
 * @Data: 2025/3/16 18:45
 * @Version: 1.0
 */


public class Main {
    public static void main(String[] args) {
//        System.out.println(jdbcSelectById(1));
//        System.out.println(jdbcSelectById(2));

        MySqlSessionFactory mySqlSessionFactory = new MySqlSessionFactory();
        UserMapper mapper = mySqlSessionFactory.getMapper(UserMapper.class);
        User user = mapper.selectById(2L);
        System.out.println(user);
        System.out.println(mapper.selectByName("代知意"));
    }

    /**
     * @Author: dh
     * @Description: 查询数据
     * @DateTime: 2025/3/16 20:15
     * @Param: [id]
     * @Return java.lang.Object
     */
    public static Object jdbcSelectById(Integer id) {
        String dbUrl = "jdbc:mysql://127.0.0.1:3306/test1";
        String dbUserName = "root";
        String dbPassword = "123456";
        String sql = "SELECT * FROM `user` WHERE id = ?";
        try {
            Connection connection = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                User user = new User();
                user.setId(resultSet.getLong("id"));
                user.setUsername(resultSet.getString("user_name"));
                user.setGender(resultSet.getInt("gender"));
                return user;
            }
            return null;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
